clear all

/* PATHS */
qui do "code/config.do"

/* Import data */
import delimited "data/FlatsWithDist.csv"

// Create variables
egen disttodt_r1 = rowmin(disttodt1 disttodt2 disttodt3 disttodt5 disttodt6 disttodt7 disttodt8 disttodt9 disttodt10 disttodt11 disttodt12 disttodt13 )
gen date= date(month,"YMD")
gen alignment_r1= date("2008-07-15","YMD")
gen quarter = qofd(date)
gen alignment_r1_q = qofd(alignment_r1)

// Collapse and organize data
gcollapse (mean) resale_price , by(flat_type postalcode quarter alignment_r1_q disttodt_r*)
egen flat_id = group(flat_type postalcode)
tsset flat_id quarter
tsfill, full
by flat_id: ipolate resale_price quarter, gen(resale_price_filled)
local vars alignment_r1_q disttodt_r1
foreach var in `vars'{
	bysort flat_id: egen `var'_filled = mode(`var')
}

// Save
save temp.dta,replace

// Event study: Figure 2b
gen log_price = log(resale_price_filled)
replace quarter = quarter - alignment_r1_q_filled
tostring quarter, gen(qtr)
keep if abs(quarter)<=20
encode qtr, gen(q)
reg log_price c.disttodt_r1_filled##ib(21).q if disttodt_r1_filled < 4 & quarter<=15 & quarter >=-5, r
estimates store results
coefplot results, vertical drop(_cons disttodt_r1_filled *.q) coeflabels(1.q#c.disttodt_r1_filled = "-1" 12.q#c.disttodt_r1_filled = "-2" 14.q#c.disttodt_r1_filled = "-3" 15.q#c.disttodt_r1_filled = "-4" 16.q#c.disttodt_r1_filled = "-5" 21.q#c.disttodt_r1_filled = "0" 22.q#c.disttodt_r1_filled = "1" 23.q#c.disttodt_r1_filled = "10" 24.q#c.disttodt_r1_filled = "11" 25.q#c.disttodt_r1_filled = "12" 26.q#c.disttodt_r1_filled = "13" 27.q#c.disttodt_r1_filled = "14" 28.q#c.disttodt_r1_filled = "15" 33.q#c.disttodt_r1_filled = "2" 35.q#c.disttodt_r1_filled = "3" 36.q#c.disttodt_r1_filled = "4" 37.q#c.disttodt_r1_filled = "5" 38.q#c.disttodt_r1_filled = "6" 39.q#c.disttodt_r1_filled = "7" 40.q#c.disttodt_r1_filled = "8" 41.q#c.disttodt_r1_filled = "9", wrap(2))  baselevels omitted nolabel ///
order(16.q#c.disttodt_r1_filled = "-5" 15.q#c.disttodt_r1_filled = "-4" 14.q#c.disttodt_r1_filled = "-3" 12.q#c.disttodt_r1_filled = "-2" 1.q#c.disttodt_r1_filled = "-1" 21.q#c.disttodt_r1_filled = "0" 22.q#c.disttodt_r1_filled = "1" 33.q#c.disttodt_r1_filled = "2" 35.q#c.disttodt_r1_filled = "3" 36.q#c.disttodt_r1_filled = "4" 37.q#c.disttodt_r1_filled = "5" 38.q#c.disttodt_r1_filled = "6" 39.q#c.disttodt_r1_filled = "7" 40.q#c.disttodt_r1_filled = "8" 41.q#c.disttodt_r1_filled = "9" 23.q#c.disttodt_r1_filled = "10" 24.q#c.disttodt_r1_filled = "11" 25.q#c.disttodt_r1_filled = "12" 26.q#c.disttodt_r1_filled = "13" 27.q#c.disttodt_r1_filled = "14" 28.q#c.disttodt_r1_filled = "15")  xtitle(Quarters from Announcement) title(Regression of Log Prices on Distance: Coefficient) ytitle(Distance from Rail) note(Prices are higher the closer flats are to the rail after the announcement)



// Event study: Figure 2a
clear all
use temp.dta

gen treat = .
replace treat = 0 if disttodt_r1_filled < 5
replace treat = 1 if disttodt_r1_filled < 1
keep if treat!=.
gen pre = quarter <= alignment_r1_q_filled & resale_price!=.
gen post = quarter > alignment_r1_q_filled & resale_price!=.
bysort flat_id: egen post_sum = sum(post)
bysort flat_id: egen pre_sum = sum(pre)
drop if post_sum == 0 | pre_sum==0
replace quarter = quarter - alignment_r1_q_filled

bysort treat quarter: egen mean_price = mean(resale_price_filled)
keep quarter treat mean_price
duplicates drop
reshape wide mean_price, i(quarter) j(treat)
gen diff= mean_price1 - mean_price0
gen log_diff = log(mean_price1) - log(mean_price0)

tsline log_diff if quarter <= 20 & quarter >-9, xline(1) ytitle(Log Price Difference) xtitle(Quarters from Alignment Opening) title(Effect of Downtown Line: Flats within 1km vs 1-5km) note("Event Announcement for Bugis - Bukit Panjang stations: July 15, 2008")

// Table A5
clear all
use temp.dta

/// Panel A
local dist_cutoff 1
local dist_max  5

gen treat = .
replace treat = 0 if disttodt_r1_filled < `dist_max' 
replace treat = 1 if disttodt_r1_filled < `dist_cutoff' 
keep if treat!=.
gen pre = quarter <= alignment_r1_q_filled & resale_price!=.
gen post = quarter > alignment_r1_q_filled & resale_price!=.
bysort flat_id: egen post_sum = sum(post)
bysort flat_id: egen pre_sum = sum(pre)
replace quarter = quarter - alignment_r1_q_filled
bysort flat_id treat quarter : egen mean_price = mean(resale_price_filled)
keep quarter flat_id treat mean_price post
gen log_price=log(mean_price)

eststo clear
eststo: areg log_price treat##post if quarter<=20 & quarter>-10, r absorb(flat_id)
eststo: areg log_price treat##post if (quarter==-1 | quarter==8 ), r absorb(flat_id)
eststo: areg log_price treat##post if (quarter==-1 | quarter==12 ), r absorb(flat_id)
eststo: areg log_price treat##post if (quarter==-1 | quarter==16 ), r absorb(flat_id)
esttab,  label star(* 0.1 ** 0.05 *** 0.01) replace se aic 


/// Panel B
local dist_cutoff 1
local dist_max  4

gen treat = .
replace treat = 0 if disttodt_r1_filled < `dist_max' 
replace treat = 1 if disttodt_r1_filled < `dist_cutoff' 
keep if treat!=.
gen pre = quarter <= alignment_r1_q_filled & resale_price!=.
gen post = quarter > alignment_r1_q_filled & resale_price!=.
bysort flat_id: egen post_sum = sum(post)
bysort flat_id: egen pre_sum = sum(pre)
replace quarter = quarter - alignment_r1_q_filled
bysort flat_id treat quarter : egen mean_price = mean(resale_price_filled)
keep quarter flat_id treat mean_price post
gen log_price=log(mean_price)

eststo clear
eststo: areg log_price treat##post if quarter<=20 & quarter>-10, r absorb(flat_id)
eststo: areg log_price treat##post if (quarter==-1 | quarter==8 ), r absorb(flat_id)
eststo: areg log_price treat##post if (quarter==-1 | quarter==12 ), r absorb(flat_id)
eststo: areg log_price treat##post if (quarter==-1 | quarter==16 ), r absorb(flat_id)
esttab,  label star(* 0.1 ** 0.05 *** 0.01) replace se aic 


/// Panel C
local dist_cutoff 1
local dist_max  3

gen treat = .
replace treat = 0 if disttodt_r1_filled < `dist_max' 
replace treat = 1 if disttodt_r1_filled < `dist_cutoff' 
keep if treat!=.
gen pre = quarter <= alignment_r1_q_filled & resale_price!=.
gen post = quarter > alignment_r1_q_filled & resale_price!=.
bysort flat_id: egen post_sum = sum(post)
bysort flat_id: egen pre_sum = sum(pre)
replace quarter = quarter - alignment_r1_q_filled
bysort flat_id treat quarter : egen mean_price = mean(resale_price_filled)
keep quarter flat_id treat mean_price post
gen log_price=log(mean_price)

eststo clear
eststo: areg log_price treat##post if quarter<=20 & quarter>-10, r absorb(flat_id)
eststo: areg log_price treat##post if (quarter==-1 | quarter==8 ), r absorb(flat_id)
eststo: areg log_price treat##post if (quarter==-1 | quarter==12 ), r absorb(flat_id)
eststo: areg log_price treat##post if (quarter==-1 | quarter==16 ), r absorb(flat_id)
esttab,  label star(* 0.1 ** 0.05 *** 0.01) replace se aic 
